﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BUS;
using System.Data;
using DTO;
namespace AirPlane
{
    public partial class QuanLyTaiKhoan : System.Web.UI.Page
    {
        protected HoiVienBUS hv = new HoiVienBUS();
        protected LoaiHoiVienBUS lhv = new LoaiHoiVienBUS();
        protected DataTable dt = new DataTable();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["islogin"] == null || (bool)Session["islogin"] == false || Session["type"].ToString() != "2")
            {

                Response.Redirect("TrangChu.aspx");
            }
            else
            {
                if (!IsPostBack)
                {
                    loadData(txtKey.Text);
                    lbThongBao.Text = "";
                }
            }

        }
        protected void loadData(string key)
        {
            string key1 = "%" + key + "%";
            dt = hv.layThongTinHoiVien(key1);
        
            
            gwTaiKhoan.DataSource = dt;
            gwTaiKhoan.DataBind();
            if (dt.Rows.Count > 0)
            {
                string s1 = "";
                s1 = "<script type ='text/javascript' >";
                s1 += " function pageLoad() {";
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    s1 += "$(function () {";
                    s1 += "$('#ContentPlaceHolder1_gwTaiKhoan_TextBox4_" + i + "').datepicker();";
                    s1 += "});";
                }
                s1 += "$(function () {";
                s1 += "$('#ContentPlaceHolder1_gwTaiKhoan_txtNgaySinh').datepicker();";
                s1 += "});";
                s1 += "}";
                s1 += "</script>";

                jquery.Text = s1;
                loadFooter();
            }
            else
                lbTaiKhoan.Text = "<span id = 'thong-bao-ket-Qua-Tim-Kiem'>Không tìm thấy kết quả nào!</span>";
        }

        protected void gwTaiKhoan_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gwTaiKhoan.PageIndex = e.NewPageIndex;
            loadData(txtKey.Text);
        }

        protected void gwTaiKhoan_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            gwTaiKhoan.EditIndex = -1;
            loadData(txtKey.Text);
        }

        protected void gwTaiKhoan_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            string maHoiVien = gwTaiKhoan.DataKeys[e.RowIndex].Value.ToString();
            hv.xoaHoiVien(maHoiVien);
            loadData(txtKey.Text);
        }

        protected void gwTaiKhoan_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            string maHoiVien = gwTaiKhoan.DataKeys[e.RowIndex].Value.ToString();
            string loaiHoiVien = ((DropDownList)gwTaiKhoan.Rows[e.RowIndex].Cells[1].FindControl("ddlLoaiHoiVien")).SelectedValue.ToString();
            string hoTen = ((TextBox)gwTaiKhoan.Rows[e.RowIndex].Cells[2].FindControl("TextBox3")).Text;
            string ngaySinh = ((TextBox)gwTaiKhoan.Rows[e.RowIndex].Cells[3].FindControl("TextBox4")).Text;
            string gioiTinh = ((DropDownList)gwTaiKhoan.Rows[e.RowIndex].Cells[4].FindControl("ddlGioiTinh")).SelectedValue.ToString(); 
            string email = ((TextBox)gwTaiKhoan.Rows[e.RowIndex].Cells[5].FindControl("TextBox6")).Text;
            
            string soDiem = ((TextBox)gwTaiKhoan.Rows[e.RowIndex].Cells[7].FindControl("TextBox8")).Text;
            string kichHoat = ((DropDownList)gwTaiKhoan.Rows[e.RowIndex].Cells[8].FindControl("ddlKichHoat")).SelectedValue.ToString();
            bool flag = true;
            if (soDiem != "")
            {
                if (soDiem.Length > 0)
                {
                    for (int i = 0; i < soDiem.Length; i++)
                    {
                        if (soDiem[i] < 46 || soDiem[i] > 57 || soDiem[i] == 47)
                        {
                            lbThongBao.Text = "<span id = 'thong-bao-ket-Qua-Tim-Kiem'>Số điểm phải là một số!</span>";
                            flag = false;
                            break;
                        }

                    }
                }
               
            }
            if (flag == true)
            {
                HoiVienDTO dto = new HoiVienDTO();
                dto.ID_hoiVien1 = maHoiVien;
                dto.HoTen = hoTen;
                dto.LoaiHoiVien = loaiHoiVien;
                dto.GioTinh = bool.Parse(gioiTinh);
                dto.NgaySinh = DateTime.Parse(ngaySinh);
                dto.Email = email;
                dto.SoDiemThuong = float.Parse(soDiem);
                dto.KichHoat = kichHoat;
                hv.capNhapHoiVien(dto);
                gwTaiKhoan.EditIndex = -1;
                loadData(txtKey.Text);
            }
        }

        protected void gwTaiKhoan_RowEditing(object sender, GridViewEditEventArgs e)
        {
            gwTaiKhoan.EditIndex = e.NewEditIndex;
           
            string gioiTinh =  ((Label)gwTaiKhoan.Rows[e.NewEditIndex].Cells[4].FindControl("Label5")).Text;
            string kichHoat = ((Label)gwTaiKhoan.Rows[e.NewEditIndex].Cells[4].FindControl("Label9")).Text;
            string loai = ((Label)gwTaiKhoan.Rows[e.NewEditIndex].Cells[4].FindControl("Label2")).Text;
            DataTable dt1 = new DataTable();
            dt1 = lhv.LoadDDLHoiVien();
            loadData(txtKey.Text);
            DropDownList ddlGioiTinh = (DropDownList)gwTaiKhoan.Rows[e.NewEditIndex].Cells[4].FindControl("ddlGioiTinh");
            DropDownList ddlKichHoat = (DropDownList)gwTaiKhoan.Rows[e.NewEditIndex].Cells[4].FindControl("ddlKichHoat");
            DropDownList ddlLoaiHoiVien = (DropDownList)gwTaiKhoan.Rows[e.NewEditIndex].Cells[4].FindControl("ddlLoaiHoiVien");
            ddlLoaiHoiVien.DataSource = dt1;
            ddlLoaiHoiVien.DataTextField = "tenLoaiHoiVien";
            ddlLoaiHoiVien.DataValueField = "ID_loaiHoiVien";
            ddlLoaiHoiVien.DataBind();
            if (bool.Parse(gioiTinh) == true)
            {
                ddlGioiTinh.SelectedIndex = 1;
            }
            else
                ddlGioiTinh.SelectedIndex = 0;
            if (kichHoat == "active")
            {
                ddlGioiTinh.SelectedIndex = 0;
            }
            else
                ddlGioiTinh.SelectedIndex = 1;
            for (int i = 0; i < dt1.Rows.Count; i++)
            {
                if (dt1.Rows[i]["tenLoaiHoiVien"].ToString().Equals(loai))
                {
                    ddlLoaiHoiVien.SelectedIndex = i;
                    break;
                }
            }
        }
        public void loadFooter()
        {
            DataTable dt1 = new DataTable();
            dt1 = lhv.LoadDDLHoiVien();
            DropDownList ddlLoaiHoiVien = (DropDownList)gwTaiKhoan.FooterRow.FindControl("ddlLoaiHoiVien_add");
            ddlLoaiHoiVien.DataSource = dt1;
            ddlLoaiHoiVien.DataTextField = "tenLoaiHoiVien";
            ddlLoaiHoiVien.DataValueField = "ID_loaiHoiVien";
            ddlLoaiHoiVien.DataBind();
        }
        protected void imgbtn_addMember_Click(object sender, ImageClickEventArgs e)
        {
            string loaiHoiVien = ((DropDownList)gwTaiKhoan.FooterRow.FindControl("ddlLoaiHoiVien_add")).SelectedValue.ToString();
            string hoTen = ((TextBox)gwTaiKhoan.FooterRow.FindControl("txtHoTen")).Text;
            string ngaySinh = ((TextBox)gwTaiKhoan.FooterRow.FindControl("txtNgaySinh")).Text;
            string gioiTinh = ((DropDownList)gwTaiKhoan.FooterRow.FindControl("ddlGioiTinh_add")).SelectedValue.ToString();
            string email = ((TextBox)gwTaiKhoan.FooterRow.FindControl("txtEmail")).Text;
            string taiKhoan = ((TextBox)gwTaiKhoan.FooterRow.FindControl("txtTaiKhoan")).Text;
           // string soDiem = ((TextBox)gwTaiKhoan.FooterRow.FindControl("txtSoDiem")).Text;
            string kichHoat = ((DropDownList)gwTaiKhoan.FooterRow.FindControl("ddlKichHoat_add")).SelectedValue.ToString();
             bool flag = true;
             if (hv.kiemTraTenDangNhap(taiKhoan) == true)
             {
                 flag = false;
                 lbThongBao.Text = "<span id = 'thong-bao-ket-Qua-Tim-Kiem'>Tên đăng nhập này đã tồn tại!</span>";

             }
             
             if (flag == true)
             {
                 HoiVienDTO dto = new HoiVienDTO();
                 dto.HoTen = hoTen;
                 dto.LoaiHoiVien = loaiHoiVien;
                 dto.GioTinh = bool.Parse(gioiTinh);
                 dto.NgaySinh = DateTime.Parse(ngaySinh);
                 dto.Email = email;
                 dto.TenDangNhap = taiKhoan;
                // dto.SoDiemThuong = float.Parse(soDiem);
                 dto.KichHoat = kichHoat;
                 hv.themHoiVien(dto);
                 loadData(txtKey.Text);
             }
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            loadData(txtKey.Text);
        }
    }
}